package ninder.system.control;
import java.util.HashMap;
import java.util.Map;

import ninder.system.model.User;





public class SessionsManager {


	private static SessionsManager manager = null;
	private Map <String, User> sessions;


	public  synchronized Map<String, User>getSessions() {
		return sessions;
	}

	
	private SessionsManager() {
		this.sessions = new HashMap<String, User>();
	}

	
	public static SessionsManager getInstance() {
		if (manager == null) {
			manager = new SessionsManager();
		}
		return manager;
	}
	

	
	public synchronized void addSession(String idSession, User user) {
		sessions.put(idSession, user);
	}

	
	public  synchronized  User getUserOfSession(String sessionID) {
		return sessions.get(sessionID);
	}


	
	public synchronized  void removeSession(String sessionID) {
		 sessions.remove(sessionID);
	}
	
    public synchronized  boolean existSession(String sessionID){
        return sessions.containsKey(sessionID);
    }
    
    public synchronized  long getNumberOfSessions(){
        return sessions.size();
    }
    
    public synchronized  Map<String, User> getAllSessions(){
        return sessions;
    }
 
    public synchronized void cleanAllSessionsOfUser(User user){
    	for(Map.Entry<String, User> entry : sessions.entrySet()) {
    		if(entry.getValue().equals(user)){
                sessions.remove(entry.getKey());
             }
    	}

    }
    


}

